﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using posmoon.Tree;
using posmoon.Data;

namespace posmoon.Forms
{
    public partial class DBRegisterForm : Form
    {
        HostTreeNode _host;
        DataProvider dp;
        List<DBTreeNode> _dbnodes;

        public List<DBTreeNode> GetDB
        {
            get { return _dbnodes; }        
        }

        public DBRegisterForm(HostTreeNode node)
        {
            InitializeComponent();
            _host = node;
            _dbnodes = new List<DBTreeNode>();
            LoadDBList();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DBTreeNode b;            

            if (lstSelected.Items.Count != 0)
            {
                foreach (ListViewItem db in lstSelected.Items)
                {
                    b = new DBTreeNode(db.Text, _host.UserName, _host.UserPassword, _host);
                    b.Register();

                    _dbnodes.Add(b);
                }
            }
            Close();
        }

        private void LoadDBList()
        {
            dp = new DataProvider(_host.Connection);

            foreach (string db in dp.GetDBsList())
                lstAvailable.Items.Add(db);
        }

        private void btnSelectOne_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem db in lstAvailable.SelectedItems)
            {
                lstSelected.Items.Add(db.Text);
                lstAvailable.Items.Remove(db);
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem db in lstAvailable.Items)
            {
                lstSelected.Items.Add(db.Text);
                lstAvailable.Items.Remove(db);
            }
        }

        private void btnDiselectAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem db in lstSelected.Items)
            {
                lstAvailable.Items.Add(db.Text);
                lstSelected.Items.Remove(db);
            }
        }

        private void btnDiselectOne_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem db in lstSelected.SelectedItems)
            {
                lstAvailable.Items.Add(db.Text);
                lstSelected.Items.Remove(db);
            }
        }
    }
}
